﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Productions;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;
using NPOI.SS.Formula.Functions;
using NPOI.Util;

namespace MangoMes.Write.API.Applications.Strippings
{
    /// <summary>
    /// 生产退料单行 新增
    /// </summary>
    public class RejectionsCreateHandler : IRequestHandler<RejectionsCreateCommand, ResultsDTO>
    {
        #region 仓储
        private readonly IBateRepository<Rejections> RejectionsRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="RejectionsRepository"></param>
        public RejectionsCreateHandler(IBateRepository<Rejections> RejectionsRepository)
        {
            this.RejectionsRepository = RejectionsRepository;
        }
        #endregion

        #region 生产退料单行 新增
        /// <summary>
        /// 生产退料单行 新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultsDTO> Handle(RejectionsCreateCommand request, CancellationToken cancellationToken)
        {
            //异常处理
            try
            {
                //初始化一个ResultDTO对象result，并设置Code为 0
                ResultsDTO result = new ResultsDTO() { Code = 0 };
                //创建DapperHelper对象 用于与数据库进行交互
                DapperHelper helper = new DapperHelper();
                //创建新实例 rejection 通过对象初始化器的方式定义一些属性赋值
                Rejections rejection = new Rejections()
                {
                    RejectionId = request.RejectionId,
                    productManageNode = request.productManageNode,
                    productManageName = request.productManageName,
                    productManageNumber = request.productManageNumber,
                    unites = request.unites,
                    ProdLot = request.ProdLot,
                    RejectCount = request.RejectCount,
                    StashId = request.StashId,
                    RejectionStatus = request.RejectionStatus,
                    StrippingNode = request.StrippingNode,
                };

                //连接mySql 
                string conn = "server=112.124.69.237;port=3306;uid=root;pwd=No6123666;database=MangoMesDB";

                //sql语句
                string str = "insert into rejections values(@RejectionId,@ProductManageNode,@ProductManageName,@ProductManageNumber,@Unites,@ProdLot,@RejectCount,@StashId,@RejectionStatus,@StrippingNode)";

                result.Code = await RejectionsRepository.Add(str, rejection, conn);
                result.Message = "生产退料单行 新增成功";

                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

    }
}
